package com.cdhkppm.service.impl;

import com.hustcad.plm.basic.log.LogEventEnum;
import com.hustcad.plm.basic.log.TyplmLogger;
import com.hustcad.plm.basic.service.TyplmNotify;
import com.hustcad.plm.pdm.user.service.TyplmUserService;
import com.ty.basic.entity.audit.AuditLogDO;
import com.ty.basic.entity.org.UserDO;
import com.ty.basic.service.snow.SnowflakeIdComponent;
import com.ty.basic.session.TyAccountContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Component
@Slf4j
public class DyLogHelper {
    @Resource
    private TyplmUserService typlmUserService;
    @Resource
    private TyplmLogger typlmLogger;
    @Resource
    private SnowflakeIdComponent snowflakeIdComponent;
    @Resource
    private TyplmNotify notify;
    public void insertIntoLog(String target, String output,String userName, LogEventEnum eventName) {
        UserDO userDO = typlmUserService.queryUserByName(userName);
        AuditLogDO logDO = new AuditLogDO();
        logDO.setOid(snowflakeIdComponent.getInstance().nextId());
        logDO.setCreatestamp(new Date());
        logDO.setCreatoroid(userDO.getOid());
        logDO.setCreatorotype(userDO.getOtype());
        logDO.setDescription(output);
        logDO.setTargetidentity(target);
        logDO.setErrorfalg(0);
        this.typlmLogger.info(logDO, eventName);
        sendCollectInfo();
    }
    private void sendCollectInfo() {
        typlmLogger.sendCollectInfo();
        notify.sendCollectInfo();
        if (!ObjectUtils.isEmpty(TyAccountContext.getBizLogHolder())) {
            TyAccountContext.cleanBizLogHolder();
        }
    }
}
